<template>
  <div class="login">

        <vue-particles
          class="login-bg"
          color="#0076c3"
          :particleOpacity="0.7"
          :particlesNumber="60"
          shapeType="circle"
          :particleSize="4"
          linesColor="#8DD1FE"
          :linesWidth="1"
          :lineLinked="true"
          :lineOpacity="0.4"
          :linesDistance="150"
          :moveSpeed="2"
          :hoverEffect="true"
          hoverMode="grab"
          :clickEffect="true"
          clickMode="push"
          >      
        </vue-particles>
     

    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" style="width:450px;height:420px">
      <!-- <h3 class="title">FusionView 数据可视化平台</h3> -->
      <img v-if="logo" :src="logo" class="logo_picture">
      
      <el-form-item v-if="loginType=='0'" prop="username" class="login-div">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号" class="login-input" style="width:95%">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      
      <el-form-item v-if="loginType=='0'" prop="password" class="login-div">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
          class="login-input"
          style="width:95%"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item v-if="loginType=='0'" prop="code" class="login-div">
        <el-input
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 60%"
          @keyup.enter.native="handleLogin"
          class="login-input"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" />
        </div>
      </el-form-item>

      <el-form-item v-if="loginType=='1'" prop="telphone" class="login-div">
        <el-input v-model.number="loginForm.telphone" type="text" auto-complete="off" placeholder="手机号" class="login-input" style="width:95%">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      
      <el-form-item v-if="loginType=='1'" prop="telphoneCode" class="login-div">
        <el-input
          v-model="loginForm.telphoneCode"
          auto-complete="off"
          placeholder="验证码"
          style="width: 60%"
          @keyup.enter.native="handleLogin"
          class="login-input"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        
        <div class="login-yzm">
         <!-- <span v-if="yzmBool==false" class="yzm" @click="getyzm">获取验证码</span>
         <span v-else class="yzm">{{this.yzmtext}}</span> -->
         <el-button  v-if="yzmBool==false" type="primary" class="yzm" @click="getyzm" plain>获取验证码</el-button>
        <el-button  v-else type="primary" class="yzm" plain>{{this.yzmtext}}</el-button>
        </div>
      </el-form-item>
     
      <el-form-item style="width:100%;" class="loginDiv">
        
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:90%;height:50%;margin-left:20px"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
      <el-form-item>
        <div class="footlogin">
          <el-checkbox v-if="loginType=='0'" v-model="loginForm.rememberMe" style="color:#BCD4EB; font-size:14px; margin:0px 20px 20px;">记住密码</el-checkbox>

          <span v-if="loginType=='1'" class="tellogin" @click="codelogin">用户名登录</span>
          <span v-if="loginType=='0'" class="tellogin" @click="tellogin">手机号登录</span>
          <span class="registered" @click="register">用户注册</span>

        </div>
      </el-form-item>
      
      
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright ? 2020-2021 工大软件-人工智能工作室 All Rights Reserved. | <a class="knowMore" href="/cms/home">[官网]了解更多</a> </span>
    </div>
  </div>
</template>

<script>
import { getCodeImg ,getSmsCode,getBindTel} from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'
import logoImg from '@/assets/image/logo5.png'

export default {
  name: "Login",
  data() {

    //  判断手机号码有效性
    let checkPhoneNumber = (rule, value, callback) => {

      let phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;
      if (!value) {
        return callback(new Error('手机号码不能为空'));

      }else if(!Number.isInteger(value)) {
        callback(new Error('请确认输入手机号码'));
        
      }else if(phoneReg.test(this.loginForm.telphone) == false) {
        //console.log("请核对您的手机号码")
        callback(new Error('请输入有效的手机号码'));

      } else {
        callback()
      }
    };

    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",    //admin
        password: "",   //admin123
        telphone: "",
        telphoneCode: "",
        rememberMe: false,
        loginType: "0",
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" }
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" }
        ],
        code: [{ required: true, trigger: "change", message: "验证码不能为空" }],

        telphone: [
            { required: true, validator: checkPhoneNumber, trigger: "blur" }
        ],
        telphoneCode: [{ required: true, trigger: "change", message: "验证码不能为空" }],
      },
      loading: false,
      redirect: undefined,
      loginType : '0',
      yzmBool: false,
      yzmtext: "60s",
      logo: logoImg,
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.loginForm.loginType = this.loginType;
          // console.log(this.loginForm)
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              //this.$router.push({ path: this.redirect || "/" });
              this.$router.push("/tool/datav/datavIndex");
            })
            .catch(() => {
              this.loading = false;
              this.getCode();
            });
        }
      });
    },
    //注册
    register() {
      this.$router.push("/register");
    },
    tellogin(){
      this.loginType = "1";
    },
    codelogin(){
      this.loginType = "0";
    },
    getyzm() {

      //  判断手机号码有效性
      let telFlag = true;
      let phoneReg = /(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/;
      
      if (!this.loginForm.telphone) {
        telFlag = false;

      }else if(!Number.isInteger(this.loginForm.telphone)) {
         telFlag = false;
        
      }else if(phoneReg.test(this.loginForm.telphone) == false) {

        telFlag = false;

      } 
      if(telFlag){

        //验证手机号是否已注册
        getBindTel(this.loginForm.telphone ).then(response => {
          if (response.code == 200) {

            let i = 60;
            let timer = setInterval(() => {
                this.yzmtext = i + 's';
                i--;
                if (i < 0) {
                    this.yzmBool = false;
                    this.yzmtext = '获取验证码';
                    clearInterval(timer);
                }
            }, 1000);
            //注册用户可发送验证码
            getSmsCode(this.loginForm.telphone ).then(response => {
              if (response.code == 200) {
                this.yzmBool = true;
              }
              else{
                this.msgError("发送手机验证码错误");
              }
            });
          }
        });
        
      } 

    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/image/star.jpg");
  background-size: cover;
  position: relative;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #BCD4EB;/** #707070; **/
  font-size: 24px;
}
.el-checkbox__label{
  color: #BCD4EB;
}
.login-form {
  z-index:10 ;
  border-radius: 6px;
  /** background: #ffffff;**/
  background-image: url("../assets/image/img_bg_dl2.png");
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.el-form-item__content .login-input .el-input__inner{
  background: #d00f0f00;
  color: #BCD4EB;
  border: 0;
  border-bottom: 1px solid #BCD4EB;
  height: 43px;
  width: 90%;
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 35%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-bg{
    width: 100%;
    height: 99%;   
    position: absolute;
}
.login-div{
  height: 55px;
  margin-left: 20px;
}
.registered {
  float: right; 
  color:#BCD4EB; 
  font-size:14px; 
  margin:0px 50px 20px;
  margin-right: 21px;
  cursor: pointer;
}
.registered:hover {
  color: #1890FF;
  cursor: pointer;
}
.tellogin {
  float: right; 
  color:#BCD4EB; 
  font-size:14px; 
  margin:0px 20px 20px;
  cursor: pointer;
}
.tellogin:hover {
  color: #1890FF;
  cursor: pointer;
}
.loginDiv .el-form-item__content{
  display: flex;
}
.footlogin{
  display:flex;
  justify-content:space-between;
  position: relative;
  bottom: 2%;
}
.yzm{
  /* color:#BCD4EB; 
  font-size:14px; 
  cursor: pointer;
  float: right; */
      margin-left: 22px;
    width: 110px ;
}
.login-yzm{
  width: 30%;
  height: 38px;
  float: right;
  margin-right: 37px;
}
.el-form-item__error {
    color: #ff4949;
    font-size: 12px;
    line-height: 1;
    padding-top: 10px;
    position: absolute;
    top: 100%;
    left: 0;
}

.knowMore {
    color: #1890FF;
}
.knowMore:hover {
  color: #46A6FF;
  cursor: pointer;
}
.logo_picture {
    margin-top: 0px;
    padding-bottom: 15px;
    padding-left: 35px;
    width: 350px;
}
</style>
